package com.ruoyi.web.controller.set;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.SetArea;
import com.ruoyi.system.domain.SetLine;
import com.ruoyi.system.service.ISetLineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 线体Controller
 *
 * @author ruoyi
 * @date 2022-11-21
 */
@RestController
@Api(tags = "线体信息")
@RequestMapping("/set/line")
public class SetLineController extends BaseController
{
    @Autowired
    private ISetLineService setLineService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询线体列表
     */
    //@PreAuthorize("@ss.hasPermi('set:line:list')")
    @ApiOperation(value = "查询线体列表")
    @GetMapping("/list")
    public TableDataInfo list(@ApiParam(value = "线体参数", required = false) SetLine setLine)
    {
        startPage();
        List<SetLine> list = setLineService.selectSetLineList(setLine);
        return getDataTable(list);
    }

    /**
     * 查询线体下拉接口
     */
    //@PreAuthorize("@ss.hasPermi('set:line:interface')")
    @ApiOperation(value = "查询线体下拉接口")
    @GetMapping("/interface")
    public AjaxResult interfaces(@ApiParam(value = "线体参数", required = false) SetLine setLine)
    {
        List<SetLine> list = setLineService.selectSetLineList(setLine);
        return AjaxResult.success(list);
    }

    /**
     * 导出线体列表
     */
    //@PreAuthorize("@ss.hasPermi('set:line:export')")
    @Log(title = "线体", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出线体列表")
    @GetMapping("/export")
    public AjaxResult export(HttpServletResponse response, @ApiParam(value = "线体参数", required = false) SetLine setLine)
    {
        List<SetLine> list = setLineService.selectSetLineList(setLine);
        ExcelUtil<SetLine> util = new ExcelUtil<SetLine>(SetLine.class);
        return util.exportExcel( list, "线体数据");
    }

    /**
     * 导入线体列表
     */
    @Log(title = "线体", businessType = BusinessType.IMPORT)
    //@PreAuthorize("@ss.hasPermi('set:line:import')")
    @ApiOperation(value = "导入线体列表")
    @PostMapping("/importData")
    public AjaxResult importData(@ApiParam(value = "导入文件", required = true) MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SetLine> util = new ExcelUtil<SetLine>(SetLine.class);
        List<SetLine> lineList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = setLineService.importLine(lineList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 导入模板
     */
    @ApiOperation(value = "导入模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        List<SetLine> list = setLineService.selectSetLineList(new SetLine());
        ExcelUtil<SetLine> util = new ExcelUtil<SetLine>(SetLine.class);
        return util.importTemplateExcel(list,"线体数据");
    }

    /**
     * 获取线体详细信息
     */
    //@PreAuthorize("@ss.hasPermi('set:line:query')")
    @ApiOperation(value = "获取线体详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "线体id", required = true) @PathVariable("id") Long id)
    {
        return AjaxResult.success(setLineService.selectSetLineById(id));
    }

    /**
     * 新增线体
     */
    //@PreAuthorize("@ss.hasPermi('set:line:add')")
    @Log(title = "线体", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增线体")
    @PostMapping
    public AjaxResult add(@ApiParam(value = "线体实体类", required = true) @RequestBody SetLine setLine)
    {
        if (UserConstants.NOT_UNIQUE.equals(setLineService.checkCodeUnique(setLine)))
        {
            return AjaxResult.error("新增线体'" + setLine.getLineName() + "'失败，线体编码已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(setLineService.checkNameUnique(setLine)))
        {
            return AjaxResult.error("新增线体'" + setLine.getLineName() + "'失败，线体名称已存在");
        }
        return toAjax(setLineService.insertSetLine(setLine));
    }

    /**
     * 修改线体
     */
    //@PreAuthorize("@ss.hasPermi('set:line:edit')")
    @Log(title = "线体", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改线体")
    @PutMapping
    public AjaxResult edit(@ApiParam(value = "线体实体类", required = true) @RequestBody SetLine setLine)
    {
        if (UserConstants.NOT_UNIQUE.equals(setLineService.checkCodeUnique(setLine)))
        {
            return AjaxResult.error("修改线体'" + setLine.getLineName() + "'失败，线体编码已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(setLineService.checkNameUnique(setLine)))
        {
            return AjaxResult.error("修改线体'" + setLine.getLineName() + "'失败，线体名称已存在");
        }
        return toAjax(setLineService.updateSetLine(setLine));
    }

    /**
     * 删除线体
     */
    //@PreAuthorize("@ss.hasPermi('set:line:remove')")
    @Log(title = "线体", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除线体")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam(value = "线体id数组", required = true) @PathVariable Long[] ids)
    {
        return toAjax(setLineService.deleteSetLineByIds(ids));
    }
}
